//
//  ZTUserInfoManage.m
//  ZTPrivateLib
//
//  Created by 武得亮 on 2021/3/16.
//

#import "ZTUserInfoManage.h"

#import "ZTMacros.h"

#import <MJExtension/MJExtension.h>
#import "NSObject+ZTNotNull.h"
#import "NSString+ZTUrl.h"
static NSString *kLoginUserIdKey = @"loginUserIdKey";
static NSString *kUserIsLoginKey = @"userIsLoginKey";

static ZTUserInfoManage *loginUserService = nil;

@implementation ZTUserInfoManage


+ (instancetype)defaultService
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loginUserService = [[ZTUserInfoManage alloc] init];
    });
    return loginUserService;
}


/**
 *  登录成功保存用户信息
 */
- (void)saveLoginUserInfo:(id)userinfo
{
    UserModel *userModel = [UserModel mj_objectWithKeyValues:userinfo];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[self processDictionaryIsNSNull:userinfo] forKey:userModel.uid];
    [userDefaults setObject:userModel.picture forKey:UserPicture];
    [userDefaults setObject:userModel.uid forKey:kLoginUserIdKey];
    [userDefaults setObject:userModel.userRoles?userModel.userRoles:@"" forKey:isSendNitification];
    [userDefaults setObject:userModel.token forKey:APPDeviceToken];
    [userDefaults setObject:userModel.telephone forKey:USERTELEPHONE];
    [userDefaults setObject:userModel.email forKey:USEREMAIL];
    [userDefaults setObject:userModel.pdId forKey:USERPDID];
    [userDefaults setObject:userModel.msgList forKey:MSGLIST];
    [userDefaults setObject:userModel.orgId forKey:USERORGID];
    [userDefaults setObject:userModel.companyId forKey:COMPANYID];
    [userDefaults setObject:userModel.account forKey:ACCOUNT];

    [userDefaults synchronize];
    
}

//判断是否包含null
- (id) processDictionaryIsNSNull:(id)obj{
    const NSString *blank = @"";
    
    if ([obj isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *dt = [(NSMutableDictionary*)obj mutableCopy];
        for(NSString *key in [dt allKeys]) {
            id object = [dt objectForKey:key];
            if([object isKindOfClass:[NSNull class]]) {
                [dt setObject:blank
                       forKey:key];
            }
            else if ([object isKindOfClass:[NSString class]]){
                NSString *strobj = (NSString*)object;
                if ([strobj isEqualToString:@"<null>"]) {
                    [dt setObject:blank
                           forKey:key];
                }
            }
            else if ([object isKindOfClass:[NSArray class]]){
                NSArray *da = (NSArray*)object;
                da = [self processDictionaryIsNSNull:da];
                [dt setObject:da
                       forKey:key];
            }
            else if ([object isKindOfClass:[NSDictionary class]]){
                NSDictionary *ddc = (NSDictionary*)object;
                ddc = [self processDictionaryIsNSNull:object];
                [dt setObject:ddc forKey:key];
            }
        }
        return [dt copy];
    }
    else if ([obj isKindOfClass:[NSArray class]]){
        NSMutableArray *da = [(NSMutableArray*)obj mutableCopy];
        for (int i=0; i<[da count]; i++) {
            NSDictionary *dc = [obj objectAtIndex:i];
            dc = [self processDictionaryIsNSNull:dc];
            [da replaceObjectAtIndex:i withObject:dc];
        }
        return [da copy];
    }
    else{
        return obj;
    }
}

// 保存用户的登录密码
- (void)savePassWord:(NSString *)pw{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:pw forKey:PASSWORD];
    [userDefaults synchronize];

}


// 保存用户的登录token
- (void)saveToken:(NSString *)token{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"f5a4c58a46510d51f60567f411fadd22" forKey:APPDeviceToken];
    
    
    [userDefaults synchronize];
}

- (void)saveSecretPassWord:(NSString *)spw{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:spw forKey:SECRETPASSWORD];
    [userDefaults synchronize];
}

- (NSString *)getSecretPassWord{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:SECRETPASSWORD];
}

- (NSString *)getPassWord{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   return [userDefaults objectForKey:PASSWORD];
}
//二维码
- (NSString *)getQR{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:USERQR];
}
//头像
- (NSString *)getUserPicture{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:UserPicture];
}
/**
 *  获取保存的用户信息
 */
- (UserModel *)getLoginUserInfo
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    UserModel *userModel;
    if ([NSString isEmptyOrNull:[self getLoginUserId]]) {
        userModel = [[UserModel alloc] init];
    }else{
        NSString *userinfo = [userDefaults objectForKey:[self getLoginUserId]];
        
        if (userinfo) {
            userModel = [UserModel mj_objectWithKeyValues:userinfo];
        }else{
            userModel = [[UserModel alloc] init];
        }

    }
    return userModel;
}


/**
 *  获取当前登录用户的ID
 */
- (NSString *)getLoginUserId
{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:ZTPrivateUserInfoKey];
     return [dict objectForKey:@"userId"];
}

/**
 *  获取当前登录的邮箱
 */
- (NSString *)getLoginUserEmail
{
    UserModel *model = [self getLoginUserInfo];
    return model.email;
}

/**
 *  获取当前登录用的账号
 */
- (NSString *)getLoginUserAccount
{
   NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:ZTPrivateUserInfoKey];
    return [dict objectForKey:@"userAccount"];
}

- (NSString *)getLoginUserName{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:ZTPrivateUserInfoKey];
     return [dict objectForKey:@"userName"];
}

/**
 *  获取当前登录用的组织id
 */
- (NSString *)getLoginUserOrgId{
    UserModel *model = [self getLoginUserInfo];
    return model.orgId ? model.orgId: @"";
}

/**
 *  获取当前登录用的最顶层组织id
 */
- (NSString *)getLoginUserCompanyId{
    UserModel *model = [self getLoginUserInfo];
    return model.companyId;
}


/**
 *  设置当前所属部门 pdId
 */
- (void )setLoginUserPdId:(NSString *)pdId{
    UserModel *model = [self getLoginUserInfo];
    model.pdId = pdId;
    NSString *infoStr = model.mj_JSONString;
    [self saveLoginUserInfo:infoStr];
}

/**
 *  获取当前所属部门的 pdId
 */
- (NSString *)getLoginUserPdId{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:ZTPrivateUserInfoKey];
     return [dict objectForKey:@"userPdId"];
}


///**
// *  存储该用户所有的部门信息
// */
//- (void )setLoginUserPdIdArray:(NSArray *)pdIdArray{
//    UserModel *model = [self getLoginUserInfo];
//    model.msgList = pdIdArray;
//    NSString *infoStr = model.mj_JSONString;
//    [self saveLoginUserInfo:infoStr];
//}
//
///**
// *  获取该用户所有的部门信息
// */
//- (NSArray *)getLoginUserPdIdArray{
//    UserModel *model = [self getLoginUserInfo];
//    return model.msgList ? model.msgList :@[];
//}


/**
 *  保存登录标识
 */
- (void)userLogin:(BOOL)isLogin
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:isLogin forKey:kUserIsLoginKey];
    [userDefaults synchronize];
}


/**
 *  获取用户是否登录
 */
- (BOOL)userIsLogin
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults boolForKey:kUserIsLoginKey];
}

/**
 *  保存用户手机号
 */
- (void)saveLoginUserPhone:(NSString *)phone
{
    UserModel *model = [self getLoginUserInfo];
    if (![model.phone isEqualToString:phone]) {
        [model.phone stringByReplacingOccurrencesOfString:model.phone withString:phone];
    }
    model.phone = phone;
    NSString *infoStr = model.mj_JSONString;
    [self saveLoginUserInfo:infoStr];
}

- (NSString *)getLoginUserPhone{
    UserModel *model = [self getLoginUserInfo];
    return model.phone;

}

/**
 *  保存用户的头像地址
 */
- (void)saveLoginUserAvatar:(NSString *)avatar
{
    UserModel *model = [self getLoginUserInfo];
    model.picture = avatar;
    NSString *infoStr = model.mj_JSONString;
    [self saveLoginUserInfo:infoStr];
}

/**
 *  保存用户的办公电话
 */
- (void)saveLoginUserTelePhone:(NSString *)telePhone
{
    UserModel *model = [self getLoginUserInfo];
    model.telephone = telePhone;
    NSString *infoStr = model.mj_JSONString;
    [self saveLoginUserInfo:infoStr];
}

/**
 *  保存用户的邮箱
 */
- (void)saveLoginUserEmail:(NSString *)email
{
    UserModel *model = [self getLoginUserInfo];
    model.email = email;
    NSString *infoStr = model.mj_JSONString;
    [self saveLoginUserInfo:infoStr];
}


/** 邮箱退出登录 */
- (void)emailLogout
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:EmailName];
    [userDefaults removeObjectForKey:EmailPicture];
    
}

@end
